package com.example.android.ouactivity.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.android.ouactivity.R;

/**
 * Created by 庆 on 2016/11/15.
 * 消息的适配器
 */

public class MessageViewAdapter extends RecyclerView.Adapter<MessageViewHolder>{
    public String[] datas = null;
    public MessageViewAdapter(String[] datas){
        this.datas = datas;
    }
    //创建新View，被LayoutManager所调用
    @Override
    public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_msg,parent,false);
        MessageViewHolder vh = new MessageViewHolder(view);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(final MessageViewHolder holder, final int position) {
        if(onItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }
        holder.textView.setText(datas[position]);
        switch (position){
            case 0:
                holder.imageView.setImageResource(R.drawable.msg);
                break;
            case 1:
                holder.imageView.setImageResource(R.drawable.msg_system);
                break;
        }

    }
    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.length;
    }
    //设置监听接口
    public interface OnItemClickListener{
        void onItemClick(View view , int position);
    }
    public OnItemClickListener onItemClickListener = null;
    public void setOnItemClickListener(OnItemClickListener listener){
        this.onItemClickListener = listener;
    }
}
